package breeze.framework.log;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;

/**
 * @author breeze
 * @date 2020年6月5日
 * @updated 2023-7-3
 * @email breeze_java@163.com 
 * @description
 * 有的模块未引入日志模块,SimpleLog凑合用一下
 * 日志级别从高到底为 : fatal,error,warn,info,debug,trace.配置后会输出高于等于当前级别的日志.
 */
public class SimpleLog{
	
	private static Map<String,org.slf4j.Logger> slf4jMap = new HashMap<String,org.slf4j.Logger>();
	private static StringBuffer sbf;
	private static DateFormat dateFormat;
	private static Map<String,Integer> logger_levels = new HashMap<String,Integer>();
	private static boolean useSlf4j;
	private static Logger loggerTest;
	public static String logger_level;
	
	static {
		logger_levels.put("fatal",1);
		logger_levels.put("error",2);
		logger_levels.put("warn",3);
		logger_levels.put("info",4);
		logger_levels.put("debug",5);
		logger_levels.put("trace",6);
		logger_level = "debug";
		useSlf4j = false;
	}
	
	public static void warn(String msg){
		log("warn",msg,0);
	}
	
	public static void info(String msg){
		log("info",msg,0);
	}
	
	public static void debug(String msg){
		log("debug",msg,0);
	}
	
	public static void trace(String msg){
		log("trace",msg,0);
	}
	
	public static void fatal(String msg){
		log("fatal",msg,0);
	}
	
	public static void error(String msg){
		log("error",msg,0);
	}
	
	
	public static void warn(String msg,int deep){
		log("warn",msg,deep);
	}
	
	public static void info(String msg,int deep){
		log("info",msg,deep);
	}
	
	public static void debug(String msg,int deep){
		log("debug",msg,deep);
	}
	
	public static void trace(String msg,int deep){
		log("trace",msg,deep);
	}
	
	public static void fatal(String msg,int deep){
		log("fatal",msg,deep);
	}
	
	public static void error(String msg,int deep){
		log("error",msg,deep);
	}
	
	private static void log(String method,String message,int deep){
		try {
			
			
			/**
			 * 	如果走的是包配置的日志级别,比较复杂,先不处理了,交给日志框架处理
			 * 	如果走的是root配置,则检查一下,节省一些效率.
			 */
			if(logger_levels.get(method).intValue() > logger_levels.get(logger_level).intValue()) {
				return;
			}
			
			//获取堆栈信息
			StackTraceElement[] stackElements = new Throwable().getStackTrace();
			String msg = ".";
			if(stackElements.length == 2){
				msg = stackElements[1].toString();
			}else if(stackElements.length == 3){
				msg = stackElements[2].toString();
			}else if(stackElements.length > 3){
				if(stackElements.length > 2 + deep) {
					msg = stackElements[2 + deep].toString();
				}else {
					msg = stackElements[2 + deep].toString();
				}
			}
			
			//NOPLogger为未加载实现类
			if(useSlf4j && null == loggerTest) {
				loggerTest = LoggerFactory.getLogger(List.class);
			}
			if(useSlf4j && loggerTest.getClass() != NOPLogger.class) {
				String cls = msg.substring(0,msg.indexOf("("));
				cls = cls.substring(0,cls.lastIndexOf("."));
				Logger logger = slf4jMap.get(cls);
				if(null == logger){
					Class<?> clss = Class.forName(cls);
					logger = LoggerFactory.getLogger(clss);
					slf4jMap.put(cls, logger);
				}
				Method logger_method = logger.getClass().getMethod(method, String.class);
				logger_method.invoke(logger, msg + " : " + message);
			}else {
				if(null == sbf) {
					sbf = new StringBuffer();
				}
				if(null == dateFormat) {
					dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
				}
				sbf.delete(0, sbf.length());
				sbf.append(dateFormat.format(new Date())).append(" ")
				   .append(method).append(" ")
				   .append(msg).append(" : ").append(message);
				System.out.println(sbf.toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static boolean isDebug() {
		if(logger_levels.get("debug").intValue() > logger_levels.get(logger_level).intValue()) {
			return false;
		}else {
			return true;
		}
	}
	
	public static String getLogger_level() {
		return logger_level;
	}

	public static void setLogger_level(String logger_level) {
		SimpleLog.logger_level = logger_level;
	}
	
	public static void main(String[] args) {
		SimpleLog.debug("测试一下试试");
		SimpleLog.info("测试一下试试");
	}
}

